package com.newbie.iSee;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;


public class ItemizedMapOverlay extends ItemizedOverlay{
	private ArrayList<MapOverlayItem> mOverlays = new ArrayList<MapOverlayItem>();
	public static final int TEXT_COLOR =-1;
	public static final int Radius = 5;
	private Context mctx;
	public ItemizedMapOverlay(Drawable defaultMarker, Context context) {
		  super(boundCenterBottom(defaultMarker));
		  mctx = context;
		 	  
		  
		// TODO Auto-generated constructor stub
	}
	//void boundCenter(android.graphics.drawable.Drawable balloon) 
	public void addOverlay(MapOverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	}
	public void addOverlay(MapOverlayItem overlay, Drawable marker) {
		boundCenterBottom(marker);
		overlay.setMarker(marker);
	    mOverlays.add(overlay);
	    populate();
	}
	@Override
	protected OverlayItem createItem(int i) {
		OverlayItem item = mOverlays.get(i);
		
		
		return item;
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mOverlays.size();
	}
	
	@Override
	protected boolean onTap(int index) {
	
	  final MapOverlayItem item = mOverlays.get(index);
	  
	  AlertDialog.Builder dialog = new AlertDialog.Builder(mctx);
	  dialog.setTitle(item.getTitle());
	  dialog.setMessage(item.getSnippet());
	  dialog.setPositiveButton("Check",
  			new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				
				//photo photo_to_sohw =((MapOverlayItem)item).get_Photo();
				// TODO Auto-generated method stub
				
				photo input_photo = item.get_Photo();
	  			Intent change = new Intent(mctx, PhotoPage.class);
				
				String pid = input_photo.pid;
				change.putExtra("pid", pid);
				
				/*
				Log.d("location","pid: "+pid);
				PhotoInfo _photoinfo = ShareTools.facebookInterface.getPhotoInfo(pid);
				Comment[] comments =  _photoinfo.CMlist;
				change.putExtra("urlPic", _photoinfo.source);
				change.putExtra("names", ShareTools.getNames(comments));
				change.putExtra("contents", ShareTools.getContents(comments));
				*/
				
				Activity activity = (Activity) mctx;
				activity.startActivityForResult(change, 0);

			}
		});
	  dialog.setNegativeButton("Leave it", 
	  		new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
			
			}
		}
	  );
	  dialog.show();
	  return true;
	}
	@Override
	public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow){
		int i;
		GeoPoint geopoint_to_draw;
		Projection projection = mapView.getProjection();
		for(i=0; i<mOverlays.size(); i++){
			geopoint_to_draw = mOverlays.get(i).getPoint();
			Point point = new Point();
			projection.toPixels(geopoint_to_draw, point);
			Paint paint =  new Paint();
			paint.setColor(TEXT_COLOR);
			paint.setAntiAlias(true);
			paint.setFakeBoldText(true);
			canvas.drawText(mOverlays.get(i).get_Photo().uname, point.x -4*Radius , point.y+3*Radius, paint);
		}
		Log.d("shit","drawing text");
		super.draw(canvas, mapView, shadow);
	}
	
}
